import { handleActions } from 'redux-actions'
import { hasResponseError } from 'utils'
// import moment from 'moment'
import { message } from 'antd'

const sortResultState = {
  list: [],
}

export const userSortResult = handleActions({
  'request userSort list'(state, action) {
    return {...state, loading: false}
  },
  'receive userSort list'(state, action) {
    // eslint-disable-next-line no-unused-vars
    const { req, res } = action.payload
    if (hasResponseError(res)) {
      message.error(res.msg, 10)
      return {...state, loading: false}
    }
    return {...res.data, loading: false}
  },
}, sortResultState)


const listResultState = {
  list: [],
}

export const userListResult = handleActions({
  'request user list'(state, action) {
    return {...state, loading: false}
  },
  'receive user list'(state, action) {
    // eslint-disable-next-line no-unused-vars
    const { req, res } = action.payload
    if (hasResponseError(res)) {
      message.error(res.msg, 10)
      return {...state, loading: false}
    }
    return {...res.data, loading: false}
  },
}, listResultState)

const queryResultState = () => ({
  keyword: {value: ''},
})

export const userListSearchQuery = handleActions({
  'update userList search query'(state, action) {
    return {...state, ...action.payload}
  },
  'reset userList search query'(state, action) {
    return {...queryResultState()}
  },
}, queryResultState())


const detailResultState = {
  list: [],
}
export const userDetailResult = handleActions({
  'request user detail'(state, action) {
    return {...state, loading: false}
  },
  'receive user detail'(state, action) {
    const { res } = action.payload
    if (hasResponseError(res)) {
      message.error(res.msg)
      return {...state, loading: false}
    }
    return {...res.data, loading: false}
  },
}, detailResultState)


const rloeResultState = {
  rloe: [],
}
export const roleDetailResult = handleActions({
  'request user rloe'(state, action) {
    return {...state, loading: false}
  },
  'receive user rloe'(state, action) {
    const { res } = action.payload
    if (hasResponseError(res)) {
      message.error(res.msg)
      return {...state, loading: false}
    }
    return {...res.data, loading: false}
  },
}, rloeResultState)


const rloeDaleteState = {
  rloe: [],
}
export const deleteUserResult = handleActions({
  'request user delete'(state, action) {
    //return { ...state, loading: false }
    const { id } = action.payload
    return state.filter(item => item.id !== id)
  },
  'receive user delete'(state, action) {
    const { res } = action.payload
    if (hasResponseError(res)) {
      message.error(res.msg)
      return {...state, loading: false}
    }
    return {...res.data, loading: false}
  },
}, rloeDaleteState)